<?php

/**
 * 查询的结果集
 *
 */
class lfRecodeSet extends ArrayObject
{

	function __construct()
	{
	
	}

	function __destruct()
	{
		if ($this->mysqlResource)
			mysql_free_result($this->mysqlResource);
	}

	/**
	 * 设置记录集对象的数据,通常由DAO对象的Fill方法调用
	 *
	 * @param resource $mysqlResource
	 * @param int $totalRecordCount
	 * @return lfRecordSet
	 */
	function SetData($mysqlResource, $totalRecordCount = 0, $pageCount = 1)
	{
		$this->SetResource($mysqlResource);
		$this->ToArray();
		$this->totalRecordCount = $totalRecordCount ? $totalRecordCount : $this->count();
		$this->pageCount = $pageCount;
		
		return $this;
	}

	/**
	 * 设置Mysql资源对象
	 *
	 * @param resource $mysqlResource
	 */
	function SetResource($mysqlResource)
	{
		if (gettype($mysqlResource) != "boolean")
		{
			$this->mysqlResource = $mysqlResource;
		}
	}

	/**
	 * 逐行读取，通常比SetData 方法效率高
	 *
	 * @return array
	 */
	function ReadLine()
	{
		if ($this->mysqlResource)
			return mysql_fetch_assoc($this->mysqlResource);
		return FALSE;
	}

	/**
	 * 将查询结果表示为二维数组
	 *
	 * @return RecordSet
	 */
	function ToArray()
	{
		$row = $this->ReadLine();
		$this->offsetSet($index, $row);
		$this->fields = array_keys($row);
		$index = 1;
		while( $row = $this->ReadLine() )
		{
			$this->offsetSet($index, $row);
			$index ++;
		}
		
		return $this;
	}

	/**
	 * 调试用
	 *
	 * @param int $width
	 */
	function PrintTable($width="100%")
	{
		echo "\n<table width=\"$width\" border=\"0\" cellspacing=\"1\" bgcolor=\"#228888\">\n";
		echo "<tr>\n";
		foreach ( $this->fields as $v )
		{
			echo "<th  bgcolor=\"#cccccc\">";
			echo $v;
			echo "</th>\n";
		}
		echo "</tr>\n";
		
		foreach ( $this as $key => $value )
		{
			echo "<tr >\n";
			foreach ( $value as $k=>$value )
			{
				echo "<td bgcolor=\"#eeeeee\">";
				echo $value;
				echo "</td>\n";
			}
			echo "</tr>";
		}
		
		echo "</table>\n";
		//		var_dump($this->);
	}

	/**
	 * 在分页查询的情况下已显示的记录数
	 *
	 * @return int
	 */
	function VISIBEL_COUNT()
	{
		return $this->count();
	}

	/**
	 * 如果执行的是分页查询，返回总记录数
	 *
	 * @return unknown
	 */
	function RECORD_COUNT()
	{
		return $this->totalRecordCount;
	}

	/**
	 * 分页查询中总页数,无分页返回1
	 *
	 * @return int
	 */
	function PAGE_COUNT()
	{
		return $this->pageCount;
	}
	
	protected $fields = array();
	protected $pageCount = 1;
	protected $totalRecordCount;
	protected $mysqlResource = NULL;
}

?>